package org.goldfish.cookied_chatroom02.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.goldfish.cookied_chatroom02.entity.DetailBean;
import org.goldfish.cookied_chatroom02.entity.PreviewBean;
import org.goldfish.cookied_chatroom02.model.ChatRoomManager;
import org.goldfish.cookied_chatroom02.model.ChatRoomManagerImpl;
import org.goldfish.cookied_chatroom02.model.ChatRoomParams;
import org.goldfish.cookied_chatroom02.model.RegisterManagerImpl;
import org.goldfish.cookied_chatroom02.utils.Utils;

import java.io.IOException;
import java.util.*;

@WebServlet(name = "chat", value = "/chat")
public class ChatServlet extends HttpServlet implements ChatRoomParams {
    ChatRoomManager manager;

    @Override
    public void init() throws ServletException {
        manager = new ChatRoomManagerImpl();
    }

    @Override
    protected void doGet(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        if (!manager.validSession(
                request, getServletContext())) {
            response.sendRedirect(".");
            return;
        }
        ChatRoomManager.MessageType type =
                manager.getMessageType(request);
        if (type == null) {
            Utils.warn("欢迎进入聊天室", request);
            type = ChatRoomManager.MessageType.SUMMARIES;
        }
        switch (type) {
            case SUMMARIES:
                List<PreviewBean> previews = manager.getPreviews(request);
                request.setAttribute(PREVIEWS, previews);
                Collection<String> receivers = manager.availableReceivers(request).keySet();
                request.setAttribute(RECEIVERS_TO_DISPLAY, new HashSet<>(receivers));
                request.getRequestDispatcher("/pages/chat.jsp").forward(request, response);
                break;
            case DETAILS:
                List<DetailBean> details = manager.getDetails(request);
                request.setAttribute(DETAILS_TO_DISPLAY, details);
                request.getRequestDispatcher("/pages/details.jsp").forward(request, response);
                break;
        }
    }

    /**
     * servlet for handling message sending
     *
     * @param req  an {@link HttpServletRequest} object that contains the request the client has made of the servlet
     * @param resp an {@link HttpServletResponse} object that contains the response the servlet sends to the client
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Map<String, String> formPostContentMap = Utils.parseFormPostInputStream(req.getInputStream());

        if (!manager.validSession(req, getServletContext())) {
            // session not recognised as a logged-in one
            resp.sendRedirect("/");
            return;
        }

        boolean success = manager.sendMessages(req,formPostContentMap);
        // add messages to session
        String message = success ? "发送成功" : "发送失败";
        Utils.warn(message,req);

        doGet(req, resp);
    }
}
